package com.jule.miaozhuapc.webSocket.server;

import com.jule.miaozhuapc.service.GameService;
import com.jule.miaozhuapc.util.SpringContextUtils;
import com.jule.miaozhuapc.webSocket.WawaControl;
import com.jule.miaozhuapc.webSocket.WebSocketSendMessageUtil;
import com.jule.miaozhuapc.webSocket.control.WawaControlWebSocket;
import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

public class WawaServerGameOverCallback {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private   GameService gameService;
    //初始化 gameService
//    static {
//        System.out.print(" 开始注入 service");
//        gameService = (GameService) SpringContextUtils.getBeanByClass(GameService.class);
//        //gameSerice service = ApplicationContext.getBean(GameService.class);
//        System.out.print(" 注入完成 service = "+ gameService);
//    }
    /**
     * 一局游戏结束时 发送消息的 锁
     */
    public static Map<Integer, Boolean> socketRoomsLock = new HashMap<>();

    @NotEmpty
    private Integer roomId;
    private String gameReturn;

    /**
     *  用于游戏结束回调
     * @param roomId  房间号
     * @param gameReturn 游戏结果 SUCCESS or END
     */
    public WawaServerGameOverCallback(Integer roomId, String gameReturn){
            this.roomId = roomId;
            this.gameReturn = gameReturn;
    }

    /**
     * 一局游戏结束时的回调
     *
     */
    public void GameOver() {

        // 发送结果
        logger.info("往app发送结果[" + roomId + "]:" + gameReturn + ",SocketStart.data.get(roomId)：" + WawaControlWebSocket.PlayData.get(roomId) + " LOCK = " + socketRoomsLock.get(roomId));
        // 给游戏玩家发送游戏结果
     //   if (!socketRoomsLock.containsKey(roomId) || socketRoomsLock.get(roomId) == null) {
            try {
                if (WawaControlWebSocket.PlayData.get(roomId) != null && WawaControlWebSocket.PlayData.get(roomId).getSession() != null) {
                    //发送结果次数
                    new WebSocketSendMessageUtil(WawaControl.END).setData(gameReturn).sendOne(WawaControlWebSocket.PlayData.get(roomId).getSession());

                    logger.info("gameService = " + gameService);
                    if(gameService == null){
                        gameService = (GameService) SpringContextUtils.getBeanByClass(GameService.class);
                        //GameService gameService = ApplicationContext.getBean(GameService.class);
                    }
                    logger.info("gameService = " + gameService);
                    //if(gameService != null) {
                        // sql update game rst
                        gameService.GameOver(WawaControlWebSocket.PlayData.get(roomId), "SUCCESS".equals(gameReturn));
                    //}
                    // 给房间列表 发送 有玩家抓到消息
                    // TODO   ALL message
                    final String toast = "恭喜" + WawaControlWebSocket.PlayData.get(roomId).getWxNickName() + "成功抓住了" + WawaControlWebSocket.PlayData.get(roomId).getWawaName();

                    // 清空房间
                    WawaControlWebSocket.PlayData.remove(roomId);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                logger.info("发生异常移除" + roomId);
                //清空 房间
                WawaControlWebSocket.PlayData.remove(roomId);
                // 给房间列表用户发送可以开始游戏
                new WebSocketSendMessageUtil(WawaControl.END).setData(gameReturn).sendList(WawaControlWebSocket.seeDatas.get(roomId));
            } finally {
//                //上锁
//                socketRoomsLock.put(roomId, true);
//                // 开启一个线程 10秒后取消上锁
//                new Thread(new Runnable() {
//
//                    @Override
//                    public void run() {
//                        // TODO Auto-generated method stub
//                        try {
//                            Thread.sleep(15 * 1000);
//                        } catch (InterruptedException e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                        logger.info("取消 上锁 roomId :" + roomId);
//                        socketRoomsLock.put(roomId, false);
//                    }
//                }).start();
            }
     //   }

    }
}
